﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace TMS
{
    public partial class NewReceiveNoteControl : UserControl
    {
        public MySqlDataAdapter MyDA = new MySqlDataAdapter();

        public NewReceiveNoteControl()
        {
            InitializeComponent();
            DateTime today = DateTime.Today;
            lblWriteDate.Text = today.ToString("d");
            lblWriteNameSurname.Text = Program.GetActiveUser.GetName().ToString() + " " + Program.GetActiveUser.GetSurname().ToString();
            fillCategory1CB();
        }

        private void fillCategory1CB() {
            string connString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;
            
            MySqlConnection mysqlCon = new MySqlConnection(connString);
            mysqlCon.Open();

            string sqlSelectAll = "SELECT * FROM category_1";
            MyDA.SelectCommand = new MySqlCommand(sqlSelectAll, mysqlCon);

            DataTable table = new DataTable();
            MyDA.Fill(table);

            DataRow row = table.NewRow();
            row["name"] = "--Изберете категорија1--";
            table.Rows.InsertAt(row, 0);
            
            cbCategory1.DataSource = table;
            cbCategory1.ValueMember = "id";
            cbCategory1.DisplayMember = "name";
            

            //DataRow dr;
            //dr = bodyPos.NewRow();
            //dr.ItemArray = new object[2] { 0, "---Select an item---" };
            //bodyPos.Rows.InsertAt(dr, 0);
            //comboBox1.DataSource = bodyPos;
            //comboBox1.DisplayMember = bodyPos.Columns[1].ColumnName;
            //comboBox1.ValueMember = bodyPos.Columns[0].ColumnName;
 
            mysqlCon.Close();
        }

        private void btnProducts_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        

        private void cbCategory1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox cmb = (ComboBox)sender;

            if (cmb.SelectedIndex == 0) {

                lblCategory2.Hide();
                cbCategory2.Hide();

                lblCategory3.Hide();
                cbCategory3.Hide();

                lblProduct.Hide();
                cbProduct.Hide();

                lblQuantity.Hide();
                txtQuantity.Hide();

                btnPrintBarcode.Hide();

            }
            else {

                string connString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;

                MySqlConnection mysqlCon = new MySqlConnection(connString);
                mysqlCon.Open();

                //ComboboxItem selectedCar = (ComboboxItem)cmb.SelectedItem;
                //MessageBox.Show(String.Format("Index: [{0}] CarName={1}; Value={2}", selectedIndex, selectedCar.Text, selecteVal));        

                string selectedCat1 = cmb.SelectedValue.ToString();

                string sqlSelectAll = "SELECT * FROM category_2 WHERE cat1_id = '" + selectedCat1 + "'";
                MyDA.SelectCommand = new MySqlCommand(sqlSelectAll, mysqlCon);

                DataTable table = new DataTable();
                MyDA.Fill(table);

                DataRow row = table.NewRow();
                row["name"] = "--Изберете категорија2--";
                table.Rows.InsertAt(row, 0);

                cbCategory2.DataSource = table;
                cbCategory2.ValueMember = "id";
                cbCategory2.DisplayMember = "name";

                lblCategory2.Show();
                cbCategory2.Show();
                
                mysqlCon.Close();
            }

        }

        private void cbCategory2_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox cmb = (ComboBox)sender;

            if (cmb.SelectedIndex == 0)
            {

                lblCategory3.Hide();
                cbCategory3.Hide();

                lblProduct.Hide();
                cbProduct.Hide();

                lblQuantity.Hide();
                txtQuantity.Hide();

                btnPrintBarcode.Hide();

            }
            else
            {

                string connString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;

                MySqlConnection mysqlCon = new MySqlConnection(connString);
                mysqlCon.Open();

                string selectedCat1 = cbCategory1.SelectedValue.ToString();
                string selectedCat2 = cmb.SelectedValue.ToString();

                string sqlSelectAll = "SELECT * " +
                                      "FROM category_3 " +
                                      "WHERE cat1_id = '" + selectedCat1 + "' " +
                                        "AND cat2_id = '" + selectedCat2 + "'";
                MyDA.SelectCommand = new MySqlCommand(sqlSelectAll, mysqlCon);

                DataTable table = new DataTable();
                MyDA.Fill(table);

                DataRow row = table.NewRow();
                row["name"] = "--Изберете категорија3--";
                table.Rows.InsertAt(row, 0);

                cbCategory3.DataSource = table;
                cbCategory3.ValueMember = "id";
                cbCategory3.DisplayMember = "name";

                lblCategory3.Show();
                cbCategory3.Show();

                mysqlCon.Close();
            }
        }

        private void cbCategory3_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox cmb = (ComboBox)sender;

            if (cmb.SelectedIndex == 0)
            {
                lblProduct.Hide();
                cbProduct.Hide();

                lblQuantity.Hide();
                txtQuantity.Hide();

                btnPrintBarcode.Hide();
            }
            else {

                string connString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;

                MySqlConnection mysqlCon = new MySqlConnection(connString);
                mysqlCon.Open();

                string selectedCat1 = cbCategory1.SelectedValue.ToString();
                string selectedCat2 = cbCategory2.SelectedValue.ToString();
                string selectedCat3 = cmb.SelectedValue.ToString();

                string sqlSelectAll = "SELECT * " +
                                      "FROM products " +
                                      "WHERE Category_1 = '" + selectedCat1 + "' " +
                                        "AND Category_2 = '" + selectedCat2 + "' " +
                                        "AND Category_3 = '" + selectedCat3 + "'";
                MyDA.SelectCommand = new MySqlCommand(sqlSelectAll, mysqlCon);

                DataTable table = new DataTable();
                MyDA.Fill(table);

                DataRow row = table.NewRow();
                row["name"] = "--Изберете продукт--";
                table.Rows.InsertAt(row, 0);

                cbProduct.DataSource = table;
                cbProduct.ValueMember = "id";
                cbProduct.DisplayMember = "name";

                lblProduct.Show();
                cbProduct.Show();

                mysqlCon.Close();

            }
        }

        private void cbProduct_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox cmb = (ComboBox)sender;

            if (cmb.SelectedIndex == 0)
            {
                lblQuantity.Hide();
                txtQuantity.Hide();

                btnPrintBarcode.Hide();
            }
            else
            {
                lblQuantity.Show();
                txtQuantity.Show();

                btnPrintBarcode.Show();
            }
        }

    }
}




//ПРИМЕР ЗА ДВА COMBOBOX
//public partial class Form1 : Form
//{
//    private string[] comboBox1Range = new[] { "A", "B", "C", "D" };
//    private string[] comboBox2RangeA = new[] { "A1", "A2", "A3", "A4" };
//    private string[] comboBox2RangeB = new[] { "B1", "B2", "B3", "B4" };
//    private string[] comboBox2RangeC = new[] { "C1", "C2", "C3", "C4" };
//    private string[] comboBox2RangeD = new[] { "D1", "D2", "D3", "D4" };

//    public Form1()
//    {
//        InitializeComponent();
//        comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
//        comboBox1.Items.AddRange(comboBox1Range);
//    }

//    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
//    {
//        string selectedValue = comboBox1.SelectedItem as string;

//        switch (selectedValue)
//        {
//            case "A":
//                comboBox2.Items.Clear();
//                comboBox2.Items.AddRange(comboBox2RangeA);
//                break;
//            case "B":
//                comboBox2.Items.Clear();
//                comboBox2.Items.AddRange(comboBox2RangeB);
//                break;
//            case "C":
//                comboBox2.Items.Clear();
//                comboBox2.Items.AddRange(comboBox2RangeC);
//                break;
//            case "D":
//                comboBox2.Items.Clear();
//                comboBox2.Items.AddRange(comboBox2RangeD);
//                break;
//        }
//    }
//}
